package uk.ac.glasgow.demosocs.impl;

import java.io.Serializable;
import java.util.SortedMap;
import java.util.TreeMap;

import uk.ac.glasgow.demosocs.Vote;

public class RankedVote implements Vote, Serializable {
		
		private TreeMap<Integer,String> preferences;
		
		public RankedVote(String... preferences){
			this.preferences = new TreeMap<Integer,String>();
			
			for (int i = 0; i < preferences.length; i++)
				this.preferences.put(i, preferences[i]);
		}
		
		private RankedVote(TreeMap<Integer,String> preferences){
			this.preferences = preferences;
		}
			
		public String getTopPreference(){
			if (size() > 0)
				return preferences.firstEntry().getValue();
			else return null;
		}
		
		public int size (){
			return preferences.size();
		}
		
		public RankedVote getTailPreferences(){
			if (size() > 1){
				Integer highestPreference = preferences.firstKey();
				Integer nextPreference = preferences.higherKey(highestPreference);
			
				SortedMap<Integer,String> lower = preferences.tailMap(nextPreference);
			
				return new RankedVote(new TreeMap<Integer,String>(lower));
			} else return null;
		}
		
		public String toString(){
			return preferences.toString();
		}
}
